package com.jdbc;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.google.gson.JsonArray;
import org.apache.hadoop.yarn.webapp.hamlet.Hamlet;
import org.junit.Test;

import java.sql.*;
import java.util.Arrays;

public class HiveJdbcDemo {

    private static String driverName = "org.apache.hive.jdbc.HiveDriver";


    @Test
    public void test2() {
        String str = "[{'id':1,'name':'zs','class':'三年二班', 'likes':['lol', '篮球']}," +
                "{'id':2,'name':'ls','class':'五年一班', 'likes':['足球', '游泳']}]";
        JSONArray jsonArray = JSON.parseArray(str);


        System.out.println(jsonArray.get(0));
        System.out.println(jsonArray.get(1));

    }

    @Test
    public void test1() {
        String str = "{'id':1,'name':'zs','class':'三年二班', 'likes':['lol', '篮球']}";
        JSONObject jsonObject = JSON.parseObject(str);

        System.out.println(jsonObject.get("id"));

        JSONArray likes = jsonObject.getJSONArray("likes");
        System.out.println(likes.get(1));

    }

    public static void main(String[] args) throws SQLException {
        try {
            Class.forName(driverName);
            Connection conn = DriverManager.getConnection("jdbc:hive2://node03:10000/default", "root", "root");
            PreparedStatement statement = conn.prepareStatement("select * from student limit ?");
            statement.setInt(1, 3);

            ResultSet rs = statement.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                System.out.println(id + "----" + name);
            }

            rs.close();
            statement.close();
            conn.close();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源

        }


    }
}
